//
//  MCMineTableviewCell.m
//  TLYL
//
//  Created by MC on 2017/6/12.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCMineTableviewCell.h"
#import "MCMineCellModel.h"
#import "MCCollectionViewFlowLayout.h"
#import "MCMineCollectionViewCell.h"
#import "MCMineTitleModel.h"
#import "UIView+MCParentController.h"
#import "MCPersonInformationViewController.h"
#import "MCTopUpRecordViewController.h"
#import "MCWithdrawRecordViewController.h"
#import "MCGameRecordViewController.h"

@interface MCMineTableviewCell ()
<
UICollectionViewDelegate,
UICollectionViewDataSource
>
@property (nonatomic,strong) UIView * view_back;
@property (nonatomic,strong) UILabel* lab_maintitle;
@property (nonatomic,strong) UIView * view_line;
@property (nonatomic,strong) UICollectionView* collectionView;
@property (nonatomic,strong) NSMutableArray*marr_Cell;


@end

@implementation MCMineTableviewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        [self initView];
    }
    return self;
}

- (void)initView{
    
    
    self.backgroundColor=[UIColor  clearColor];

    
    _view_back=[[UIView alloc]init];
    [self addSubview:_view_back];
    _view_back.backgroundColor=[UIColor whiteColor];
    _view_back.layer.masksToBounds = YES;
    _view_back.layer.cornerRadius = 6.0;
    _view_back.frame=CGRectMake(10, 10, G_SCREENWIDTH-20, 105);
    
    
    _lab_maintitle= [UILabel new];
    _lab_maintitle.layer.masksToBounds = YES;
    _lab_maintitle.textColor=RGB(177, 177, 177);
    _lab_maintitle.font = [UIFont systemFontOfSize:12];
    _lab_maintitle.numberOfLines=1;
    _lab_maintitle.text = @"加载中...";
    _lab_maintitle.textAlignment=NSTextAlignmentLeft;
    [_view_back addSubview:_lab_maintitle];
    [_lab_maintitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_view_back).offset(6);
        make.left.equalTo(_view_back).offset(10);
        make.width.mas_equalTo(G_SCREENWIDTH/2.0);
        make.height.mas_equalTo(20);
    }];
    
    /*
     * 画线
     */
    _view_line=[[UIView alloc]init];
    _view_line.backgroundColor=RGB(200, 200, 200);
    [_view_back addSubview:_view_line];
    [_view_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_view_back.mas_left).offset(10);
        make.right.equalTo(_view_back.mas_right).offset(-10);
        make.height.mas_equalTo(0.8);
        make.top.equalTo(_lab_maintitle.mas_bottom);
    }];
    
    [_view_back addSubview:self.collectionView];
    self.collectionView.backgroundColor=[UIColor clearColor];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_view_back.mas_left);
        make.right.equalTo(_view_back.mas_right);
        make.top.equalTo(_view_back.mas_top).offset(27);
        make.bottom.equalTo(_view_back.mas_bottom);
    }];

    
    _marr_Cell=[[NSMutableArray alloc]init];
    [_marr_Cell    removeAllObjects];
    
    
}
/*
 * collectionView
 */
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        
        //创建一个layout布局类
        MCCollectionViewFlowLayout * layout = [[MCCollectionViewFlowLayout alloc]init];
        //设置布局方向为横向流布局
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView=[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor=[UIColor clearColor];
        _collectionView.dataSource=self;
        _collectionView.delegate=self;
        [_collectionView registerClass:[MCMineCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([MCMineCollectionViewCell class])];
        
    }
    return _collectionView;
}



#pragma mark - <UICollectionViewDataSource>
// 设置headerView和footerView的
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        return nil ;
    }
    return nil ;
}
//// 设置section头视图的参考大小，与tableheaderview类似
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    BKIntegralCollectionModel * model=self.marr_Cell[section];
//    return model.header_size;
//}
//设置section的颜色
- (UIColor *)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout colorForSectionAtIndex:(NSInteger)section{
    CollectionModel * model=self.marr_Cell[section];
    return model.section_color;
}
//设置item的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionModel * model=self.marr_Cell[indexPath.section];
    return model.item_size;
}
//设置section的边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    CollectionModel * model=self.marr_Cell[section];
    return model.section_Edge;
}
// 两个cell之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    CollectionModel * model=self.marr_Cell[section];
    return model.interitemSpacing;
}
// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    CollectionModel * model=self.marr_Cell[section];
    return model.lineSpacing;
}

//numberOfItemsInSection
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    CollectionModel * model=self.marr_Cell[section];
    if ([model.id_dentifier isEqualToString:NSStringFromClass([MCMineCollectionViewCell class])]) {
        return self.marr_Cell.count;
    }
    return 0;
}

//numberOfSections
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

//UICollectionViewCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell*  cell=nil;
    CollectionModel * model=self.marr_Cell[indexPath.section];
    if (model.id_dentifier) {
        cell =[collectionView dequeueReusableCellWithReuseIdentifier:model.id_dentifier forIndexPath:indexPath];
    }
    if ([model.id_dentifier isEqualToString:NSStringFromClass([MCMineCollectionViewCell class])]) {
        
        MCMineCollectionViewCell *ex_cell=(MCMineCollectionViewCell *)cell;
        NSArray * arr=(NSArray*)model.userInfo;
        if (arr.count>indexPath.row) {
            ex_cell.dataSource=[model.userInfo objectAtIndex:indexPath.row];
        }
    }
    return cell;
}
#pragma mark-didSelect
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionModel * model=self.marr_Cell[indexPath.section];
    
    if (self.collectionViewCellDidSelectedBlock) {
        self.collectionViewCellDidSelectedBlock(model);
    }
    if ([model.id_dentifier isEqualToString:NSStringFromClass([MCMineCollectionViewCell class])]) {
        if (model.userInfo) {
            if ([self.delegate respondsToSelector:@selector(MCMineCell_Click:)]) {
                [self.delegate MCMineCell_Click:[model.userInfo objectAtIndex:indexPath.row]];
            }
        }
    }
}
-(void)setDataSource:(id)dataSource{
    _dataSource=dataSource;
   
    
    if (_marr_Cell.count>0) {
        
    }else{
        MCMineTitleModel * titlemodel =dataSource;
        _lab_maintitle.text=titlemodel.maintitle;
        for (int i=0; i<titlemodel.arr_subtitle.count; i++) {
            CollectionModel * model0=[[CollectionModel alloc]init];
            model0.header_size=CGSizeMake(G_SCREENWIDTH, 40);
            CGFloat W=(G_SCREENWIDTH-20)/4.0;
            model0.item_size=CGSizeMake(W, 78);
            model0.section_color=[UIColor clearColor];
            model0.section_Edge=UIEdgeInsetsMake(0, 0, 0, 0);
            model0.interitemSpacing=0.1;
            model0.lineSpacing=0.0;
            model0.isHaveHeader=YES;
            model0.id_dentifier=NSStringFromClass([MCMineCollectionViewCell class]);
            /*
             * info
             */
            model0.userInfo=titlemodel.arr_subtitle;
            [_marr_Cell addObject:model0];
        }
    }

    [self.collectionView reloadData];
}


- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    
}
+(CGFloat)computeHeight:(id)info{
    return 105+10;
}
@end







































